import {
  fetchOrderList,
  fetchOrderDetail,
  updateOrderList,
  uploadTemplate,
  updateOrders,
  fetchAllOrderList
} from '../services/API';
import { message } from 'antd';
export default {
  namespace: 'returnManagement',

  state: {
    dataList: [],
    tabCurrent: '', //选中的tab
    curItem: null, //当前选中订单
    showDrawer: false,
    searchData: {
      pageSize: 10,
      pageNum: 1,
      status: '',
      sort: 'DESC',
      skuNumber: '',
      orderNumber: '',
      orderDate: ''
    },
    pagination: {
      total: 0,
      current: 1,
      pageSize: 10
    }
  },

  effects: {
    // 查询列表
    *fetch({ payload }, { call, put, select }) {
      const searchData = yield select(state => {
        Object.assign(state.returnManagement.searchData, payload);
        return state.returnManagement.searchData;
      });
      const tabCurrent = yield select(state => {
        return state.returnManagement.tabCurrent;
      });
      const isAllOrders = tabCurrent === '/allorders';
      const res = yield call(isAllOrders ? fetchAllOrderList : fetchOrderList, searchData);
      if (res.code === 200) {
        let dataList, total, current, pageSize;
        if (isAllOrders) {
          dataList = res.data.list;
          total = res.data.total;
          current = res.data.pageNum;
          pageSize = res.data.pageSize;
        } else {
          dataList = res.data.data;
          total = res.data.count;
          current = searchData.pageNum;
          pageSize = searchData.pageSize;
        }
        yield put({
          type: 'save',
          payload: {
            dataList,
            searchData: {
              pageNum: searchData.pageNum,
              pageSize: searchData.pageSize,
              status: searchData.status,
              sort: searchData.sort,
              skuNumber: searchData.skuNumber,
              orderNumber: searchData.orderNumber,
              orderDate: searchData.orderDate
            },
            pagination: {
              total,
              current,
              pageSize
            }
          }
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            dataList: [],
            searchData: {
              pageNum: 1,
              pageSize: 10,
              status: searchData.status,
              sort: searchData.sort,
              skuNumber: searchData.skuNumber,
              orderNumber: searchData.orderNumber,
              orderDate: searchData.orderDate
            },
            pagination: {
              total: 0,
              current: 1,
              pageSize: 10
            }
          }
        });
      }
    },
    //添加
    *add({ payload }, { call, put, select }) {
      //  const menuDatas = yield call(getMenu,payload);
    },
    // 修改
    *update({ payload }, { call, put, select }) {
      const res = yield call(updateOrderList, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
      }
    },
    *uploadTemplate({ payload }, { call, put, select }) {
      const res = yield call(uploadTemplate, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
        yield put({
          type: 'fetch'
        });
      }
    },
    // 审核退货订单
    *updateOrders({ payload, callback }, { call, put, select }) {
      const res = yield call(updateOrders, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
        yield put({
          type: 'fetch'
        });
        yield put({
          type: 'save',
          payload: {
            curItem: null,
            showDrawer: false
          }
        });
        callback && callback();
      }
    },
    // 查询详情
    *fetchDetail({ payload, callback }, { call, put, select }) {
      const res = yield call(fetchOrderDetail, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
      }
      callback && callback();
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        dispatch({
          type: 'save',
          payload: {
            tabCurrent: pathname
          }
        });
        if (pathname === '/allreturns') {
          dispatch({
            type: 'fetch',
            payload: {
              status: ''
            }
          });
        } else if (pathname === '/allorders') {
          dispatch({
            type: 'fetch',
            payload: {
              status: ''
            }
          });
        } else if (pathname === '/Pending') {
          dispatch({
            type: 'fetch',
            payload: {
              status: 1
            }
          });
        } else if (pathname === '/Approved') {
          dispatch({
            type: 'fetch',
            payload: {
              status: 2
            }
          });
        } else if (pathname === '/toBerefunded') {
          dispatch({
            type: 'fetch',
            payload: {
              status: 8
            }
          });
        } else if (pathname === '/Rejected') {
          dispatch({
            type: 'fetch',
            payload: {
              status: 5
            }
          });
        } else if (pathname === '/Refunded') {
          dispatch({
            type: 'fetch',
            payload: {
              status: 6
            }
          });
        }
      });
    }
  }
};
